import os
import codecs
import configparser
import json


class ReadConfig:

    def __init__(self, env):
        if env == 'test':
            # 当前目录
            proDir = os.path.split(os.path.realpath(__file__))[0]
            configPath = os.path.join(proDir, "config.ini")
            fd = open(configPath, encoding='utf-8')
            data = fd.read()
            fd.close()
        elif env == 'pre':
            # 当前目录
            proDir = os.path.split(os.path.realpath(__file__))[0]
            configPath = os.path.join(proDir, "config_pre.ini")
            fd = open(configPath, encoding='utf-8')
            data = fd.read()
            fd.close()
        elif env == 'prod':
            proDir = os.path.split(os.path.realpath(__file__))[0]
            configPath = os.path.join(proDir, "config_prod.ini")
            fd = open(configPath, encoding='utf-8')
            data = fd.read()
            fd.close()

        # 这种方式可以处理URL中有%号的问题
        self.cf = configparser.RawConfigParser()

        #  读取config.ini文件
        self.cf.read(configPath,encoding='UTF-8')

    #  定义方法，获取config分组下指定name的值
    def get_email(self, name):
        value = self.cf.get("EMAIL", name)
        return value

    def get_url(self, name):
        value = self.cf.get("URL", name)
        return value

    def get_db(self, name):
        value = self.cf.get("DATABASE", name)
        return value

    def get_excel(self, name):
        value = self.cf.get("EXCEL", name)
        return value

    def get_body(self, name):
        value = self.cf.get("body", name)
        return json.loads(value)



if __name__ == '__main__':
    pass